package com.example.springbootwl08;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.util.Collections;

/**
 * @author lx
 * @create 2024-03-01 17:35
 * project_name:springboot-wl-08
 * @Description
 */

@SpringBootTest
public class Mybatis_code {
    @Test
    public void testGenCode1(){
        String moduleName = "goods";//父包模块名,重要，因为mapper不能生成模块名，可以共用此变量
        String javaResource = "C:\\Users\\L1380\\Desktop\\java高级\\M-ssm\\ssm\\springboot\\src\\main\\java";//此处请制定蓝色的java根路径
        String resourceMapper = "C:\\Users\\L1380\\Desktop\\java高级\\M-ssm\\ssm\\springboot\\src\\main\\resources\\mapper";//此处指定resource的mapper路径
        String packageName = "com.example.springbootwl08";//此处是生成的包路径
        String[] tablesNames = {"sp_goods"};

        //数据源
        DataSourceConfig dataSourceConfig = new
                DataSourceConfig.Builder("jdbc:mysql://localhost:3306/mydb", "root", "123456")
                .dbQuery(new MySqlQuery())
                .typeConvert(new MySqlTypeConvert())
                .keyWordsHandler(new MySqlKeyWordsHandler())
                .build();

        AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfig);
        //全局配置
        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .outputDir(javaResource)
                .author("ls")
                .dateType(DateType.TIME_PACK)
                .commentDate("yyyy-MM-dd")
                .build();
        //包配置
        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent(packageName)
                .moduleName(moduleName)
                .entity("entity")
                .service("service")
                .serviceImpl("service.impl")
                .mapper("mapper")
                .xml("mapper.xml")
                .controller("controller")
                .other("other")
                .pathInfo(Collections.singletonMap(OutputFile.xml, resourceMapper + File.separator + moduleName))
                .build();

        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig.Builder()
                .enableCapitalMode()
                .enableSkipView()
                .disableSqlFilter()
                .addInclude(tablesNames)
                .entityBuilder()
                .enableLombok()
                .enableTableFieldAnnotation()
                .naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel).addTableFills(new Column("create_time", FieldFill.INSERT))
                .addTableFills(new Property("update_time", FieldFill.INSERT_UPDATE))
                .idType(IdType.AUTO)
                .mapperBuilder()
                .superClass(BaseMapper.class)
                .enableBaseResultMap()
                .enableBaseColumnList()
                .build();
        autoGenerator.global(globalConfig)
                .packageInfo(packageConfig)
                .strategy(strategyConfig)
        ;
        autoGenerator.execute();
    }
}
